import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { NzIconService } from 'ng-zorro-antd/icon';
import { zip } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { MenuService } from 'src/app/layout/service/menu.service';
import { ICONS } from '../../../style-icons';
import { ICONS_AUTO } from '../../../style-icons-auto';
import { SettingsService } from './settings.service';

/**
 * 用于应用启动时
 * 一般用来获取应用所需要的基础数据等
 */
@Injectable()
export class StartupService {
  constructor(
    iconSrv: NzIconService,
    private menuSrc: MenuService,
    private httpClient: HttpClient,
    private settingsService: SettingsService
  ) {
    console.log('111')
    iconSrv.addIcon(...ICONS_AUTO, ...ICONS);
  }

  /**
   * 页面首次打开，初始化
   */
  load(): Promise<void> {
    // only works with promises
    // https://github.com/angular/angular/issues/15088
    return new Promise((resolve) => {
      zip(this.httpClient.get(`assets/tmp/i18n/zh-CN.json`), this.httpClient.get('assets/tmp/app-data.json'))
        .pipe(
          // 接收其他拦截器后产生的异常消息
          catchError((res) => {
            console.warn(`StartupService.load: Network request failed`, res);
            resolve();
            return [];
          }),
        )
        .subscribe(
          ([langData, appData]) => {
            // setting language data
            this.menuSrc.menuFormat(appData.menu);
            this.settingsService.setLayout('collapsed', false);
          },
          () => {},
          () => {
            resolve();
          },
        );
    });
  }

  loadMenu(resolve: Function, userId: string) {
    
  }
}
